package LeetCode.剑指Offer._220531.剑指offer;

import java.util.Arrays;
import java.util.Stack;

/**
 * @Author: yqy
 * @Date: 2022/05/31/14:07
 * @Description:从尾到头打印链表
 */
public class _06 {
    public static void main(String[] args) {
        ListNode node1 = new ListNode(3);
        ListNode node2 = new ListNode(234);
        ListNode node3 = new ListNode(323);
        ListNode node4 = new ListNode(31);
        node2.next=node4;
        node1.next=node3;
        node3.next=node2;

        int[] print = reversePrint(node1);
        System.out.println(Arrays.toString(print));
    }
    public static int[] reversePrint(ListNode head) {
        Stack<ListNode> stack=new Stack<>();
        ListNode temp = head;
        while (temp!=null){
            stack.push(temp);
            temp=temp.next;
        }
        int size = stack.size();
        int[] nodes=new int[size];
        for (int i = 0; i < size; i++) {
            nodes[i]=stack.pop().val;
        }
        return nodes;
    }
}
